﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DAL;

namespace BLL
{
    public class MonHocBUO:IBUO<tbl_MonHoc>
    {
        CSDLQLDiemHSC3DataContext context = new CSDLQLDiemHSC3DataContext();

        public bool Insert(tbl_MonHoc obj)
        {
            try
            {
                context.tbl_MonHocs.InsertOnSubmit(obj);
                context.SubmitChanges();
                return true;
            }
            catch
            {
                return false;
            }
        }

        public bool Delete(string id)
        {
            try
            {
                tbl_MonHoc obj = context.tbl_MonHocs.Where(p => p.id_MonHoc.Equals(id)).FirstOrDefault();
                context.tbl_MonHocs.DeleteOnSubmit(obj);
                context.SubmitChanges();
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }

        public bool Delete(tbl_MonHoc obj)
        {
            throw new NotImplementedException();
        }

        public List<tbl_MonHoc> GetAll()
        {
            throw new NotImplementedException();
        }

        public tbl_MonHoc GetSingle(string id)
        {
            throw new NotImplementedException();
        }

        public String getMonHoc(String id)
        {
            return context.tbl_MonHocs.Where(p => p.id_MonHoc.Equals(id)).Select(m => m.TenMonHoc).FirstOrDefault();
        }

        public String getKhoi(String id)
        {
            return context.tbl_Khois.Where(p => p.id_Khoi.Equals(id)).Select(m => m.TenKhoi).FirstOrDefault();
        }

        public bool Update(string id, tbl_MonHoc obj)
        {
            tbl_MonHoc mh = context.tbl_MonHocs.Where(p => p.id_MonHoc.Equals(id)).FirstOrDefault();
            mh.id_Khoi = obj.id_Khoi;
            mh.TenMonHoc = obj.TenMonHoc;

            context.SubmitChanges();
            return true;
        }

        public IQueryable HienThiMon()
        {
            var list = context.tbl_MonHocs.Join(
                    context.tbl_Khois,
                    a => a.id_Khoi,
                    b => b.id_Khoi,
                    (a, b) => new
                    {
                        id_MonHoc = a.id_MonHoc,
                        id_Khoi = b.id_Khoi,
                        TenMonHoc = a.TenMonHoc,
                        TenKhoi = b.TenKhoi
                    }
                );

            return list;
        }

        public IQueryable TimTheotenMon(String ten)
        {
            var list = context.tbl_MonHocs.Join(
                        context.tbl_Khois,
                        a => a.id_Khoi,
                        b => b.id_Khoi,
                        (a, b) => new
                        {
                            id_MonHoc = a.id_MonHoc,
                            id_Khoi = b.id_Khoi,
                            TenMonHoc = a.TenMonHoc,
                            TenKhoi = b.TenKhoi
                        }
                    ).Where(p => p.TenMonHoc.Trim().Contains(ten));

            return list;
        }

        public IQueryable TimTheoKhoi(String khoi)
        {
            var list = context.tbl_MonHocs.Join(
                        context.tbl_Khois,
                        a => a.id_Khoi,
                        b => b.id_Khoi,
                        (a, b) => new
                        {
                            id_MonHoc = a.id_MonHoc,
                            id_Khoi = b.id_Khoi,
                            TenMonHoc = a.TenMonHoc,
                            TenKhoi = b.TenKhoi
                        }
                    ).Where(p => p.TenKhoi.Trim().Contains(khoi));

            return list;
        }

        public List<tbl_Khoi> getKhoi()
        {
            return context.tbl_Khois.ToList();
        }

        public tbl_Khoi getKhoi1(String id)
        {
            return context.tbl_Khois.Where(p => p.id_Khoi.Equals(id)).FirstOrDefault();
        }

        public String getIDMon(String idKh, String tenMon)
        {
            return context.tbl_MonHocs.Where(p => ((p.id_Khoi.ToLower().Trim().ToString().Equals(idKh.ToLower().Trim().ToString())) && (p.TenMonHoc.ToLower().Trim().ToString().Equals(tenMon.ToLower().Trim().ToString())))).Select(n => n.id_MonHoc).FirstOrDefault();
        }
    }
}
